home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Day Cry
/
Day Cry CD.bin
/
oh_towns
/
taropyon
/
splib
/
splib.lzh
/
PRG
/
LHX
/
MAIN.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-12-11
|
10KB
|
543 lines
/***********************************************************
mn.c -- main module of LH
***********************************************************/
#include <loader.h>
#include <fslib.h>
#include "lh386.h"
#include <ctype.h>
#include <stdio.h>
#include <io.h>
#include <dos.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "lh.h"
#include "errmes.h"
#include "intrface.h"
#ifdef __HIGHC__
# pragma On(Align_labels);
#endif
#define SECURITY 0
#define CRCPOLY 0xA001 /* CRC-16 */
char work[4096];
char lhtmp1[] = "LHTMP$$$.)1(";
char lhtmp2[] = "LHTMP$$$.)2(";
FILE *redirect = NULL, *response = NULL;
int paramcount = -2;
char cmd;
int cmdupdate;
char *env;
char *workdir = "";
char *pager = "LESS";
char *keyword = "";
char arcname[MAX_PATH];
char filename1[MAX_PATH];
char filename2[MAX_PATH];
char filename3[MAX_PATH];
FILE *file1, *file2, *file3;
int errorlevel = 0;
#ifndef __HIGHC__
static time_t arcstamp;
#else
static time_t arcstamp =
{0};
#endif
boolean verify_mode = FALSE;
/* :*****************************************************************
: display usages
:*****************************************************************
*/
void usage(void)
{
extern void dispusage(void);
dispusage();
}
void update_arcstamp(void)
{
if (arcstamp < hpb.utc)
arcstamp = hpb.utc;
}
static void set_arcstamp(FILE * f)
{
if (flg_t)
setfiletime(f, arcstamp);
}
static char *readword(FILE * file)
{
char *p;
int c;
while (((c = getc(file)) & 0xFF) <= ' ');
if (c < 0)
{
FS_fclose(file);
return NULL;
}
p = work;
do
{
*p++ = c;
} while ((c = getc(file)) > ' ');
*p++ = '\0';
return work;
}
static void disparcname(char *title)
{
if (flg_n == 0)
{
LHX_printf( "\n%s archive : %s\n\n", title, arcname);
}
}
static void executecmd(void)
{
int modified, err;
char *bdir, *path;
long size;
uchar c;
err = 0;
arcstamp = 0;
modified = 0;
if (cmdupdate)
{
strcpy(filename1, arcname);
strcpy(backpath(filename1), lhtmp1);
if ( FS_remove(filename1) && errno != ENOENT )
error(RDONLY, filename1);
FS_rename(arcname, filename1);
} else
{
strcpy(filename1, arcname);
}
file1 = myeopen(filename1, "rb", NULL);
if (cmdupdate || cmd == 'S')
{
if (flg_w)
{
strcpy(filename2, workdir);
convdelim(filename2, DELIM);
c = filename2[strlen(filename2) - 1];
if (c != DELIM && c != ':')
strcat(filename2, DELIMSTR);
strcat(filename2, lhtmp2);
} else
{
strcpy(filename2, arcname);
strcpy(backpath(filename2), lhtmp2);
}
file2 = myeopen(filename2, "wb", NULL);
if (file2 == NULL)
{
file2 = myeopen(lhtmp2, "wb", NULL);
if (file2 == NULL)
error(MKTMPERR, filename2);
strcpy(filename2, lhtmp2);
}
}
switch (cmd)
{
case 'V':
flg_x = 1;
cmd = 'L';
case 'L':
disparcname("Listing of");
initlist();
break;
case 'A':
flg_c = 1;
cmd = 'U';
case 'U':
case 'M':
if (file1)
{
disparcname("Updating");
} else
{
disparcname("Creating");
}
mklist();
break;
case 'F':
disparcname("Freshening");
break;
case 'D':
disparcname("Deleting from");
break;
case 'X':
cmd = 'E';
case 'E':
case 'P':
disparcname("Extracting from");
break;
case 'T':
disparcname("Testing");
verify_mode = TRUE;
break;
}
/************/
if (file1)
{
inithdr();
while (file1 && (path = gethdr(&err)) != NULL)
{
bdir = matchpat(path);
switch (cmd)
{
case 'L':
if (bdir)
{
list();
}
break;
case 'U':
case 'M':
modified += append();
break;
case 'F':
modified += freshen(bdir);
break;
case 'D':
if (bdir)
{
LHX_printf("Deleted %s\n", path);
modified++;
} else
{
copylzh();
}
break;
case 'E':
case 'P':
case 'T':
if (bdir)
{
extract(bdir);
}
break;
}
free(path);
}
} else
{
if (!cmdupdate)
error(NOARCERR, arcname);
}
/************/
if (!err)
{
switch (cmd)
{
case 'L':
endlist(getfiletime(file1));
break;
case 'U':
modified += endappend();
break;
case 'M':
modified += endappend();
deletefiles();
break;
}
}
#if SECURITY
if (cmd == 'T')
{
extern int security(void);
if (security())
err = 0;
}
#endif
if (err)
error(BROKENARC, arcname);
if (cmdupdate)
{
if (modified == 0)
{
FS_fclose(file2);
if (file1)
{
set_arcstamp(file1);
FS_fclose(file1);
}
FS_remove(filename2);
FS_rename(filename1, arcname); /* restore the old archive */
} else
{
LHX_fputc('\0', file2);
size = ftell(file2);
set_arcstamp(file2);
if (file1)
FS_fclose(file1);
FS_fclose(file2);
if ( FS_rename(filename2, arcname) )
{
if (diskspace(filename1) < size)
error(NOSPCERR, NULL);
LHX_printf( "Copying TMP to ARC ... ");
FS_remove(filename1);
file1 = mywopen(arcname);
file2 = myropen(filename2);
copyfile(file2, file1, size, 0);
set_arcstamp(file1);
FS_fclose(file1);
FS_fclose(file2);
FS_remove(filename2);
LHX_printf( "done.\n" );
} else
{
FS_remove(filename1);
}
if (size == 1)
FS_remove(arcname);
}
} else
{
FS_fclose(file1);
}
}
static int execute(void)
{
struct find_t findbuf;
int nofile;
nofile = _dos_findfirst(arcname, 0x07, &findbuf);
if (nofile && strchr("AUM", cmd) == NULL)
error(NOARCERR, arcname);
if (cmdupdate)
{
if ( strpbrk(arcname, "*?") )
error(NOARCERR, arcname);
/* when updating archive, wild cards can't used */
if (!nofile && findbuf.attrib & 0x01)
error(RDONLY, arcname);
executecmd();
} else
{
do
{
backpath(arcname);
strcat(arcname, findbuf.name);
executecmd();
} while (_dos_findnext(&findbuf) == 0);
}
return errorlevel;
}
#ifdef __HIGHC__
#define GETENV(env) _hc_getenv(env)
char *_hc_getenv(const char *env)
{
char *p, *tmp;
if ((p = getenv(env)) == NULL)
return (NULL);
if ((tmp = malloc(strlen(p) + 1)) == NULL)
return (NULL);
strcpy(tmp, p);
return (tmp);
}
#else
#define GETENV(env) getenv(env)
#endif
jmp_buf MnJmpBuf;
void main( int _argc, char *_argv[] )
{
uchar c, *p, *q;
int i, argc;
char **argv;
static fileinputflag;
static char *basedir;
fileinputflag = 0;
basedir = "";
#ifdef _STARTUP_INIT
main_ini();
#endif
/* âVâXâeâÇÅëè·ë╗ */
if ( USYS_init( &argc, &argv, _argc, _argv ) )
{ pcl_exit(0);
exit(1);
}
if ( (i = setjmp(MnJmpBuf)) > 0 )
goto END_EXIT;
initbreak();
getswchar();
make_crctable(CRCPOLY);
initpat();
if ((p = (uchar *) GETENV("TMP")) != NULL
&& (p = (uchar *) strtok((char *) p, " \t")) != NULL)
{
workdir = (char *) p;
flg_w = 1;
}
if ((env = GETENV("LH")) == NULL)
env = GETENV("LHARC");
argc--;
argv++;
while (1)
{
if (env)
{
while (*env && (uchar) * env <= ' ')
env++;
if (*env == '\0')
{
env = NULL;
continue;
}
p = (uchar *) env;
while (*env > ' ')
env++;
if (*env)
{
*env++ = '\0';
}
} else if (response)
{
fileinputflag = 1;
if ((p = (uchar *) readword(response)) == NULL)
{
response = NULL;
continue;
}
} else if (argc)
{
argc--;
p = (uchar *) * argv++;
} else if (redirect == NULL || (p = (uchar *) readword(redirect)) == NULL)
{
break;
}
if (*p == swchar || *p == '-')
{
getopt((char *) ++p);
} else if (*p == '@')
{
p++;
if (response)
error(NESTERR, (char *) p);
if ((response = FS_fopen( (char *) p, "rt")) == NULL)
error(RDERR, (char *) p);
continue;
} else
{
if (paramcount == -2)
{
/* get comma